package com.qlslylq.ad.sdk.net.api;

import android.text.TextUtils;

import com.qlslylq.ad.sdk.application.AdMergeSDKApp;
import com.qlslylq.ad.sdk.constant.AppConst;
import com.qlslylq.ad.sdk.method.BaseMethod;
import com.qlslylq.ad.sdk.model.req.AppBehaviorAddDTO;
import com.qlslylq.ad.sdk.model.req.UserBehaviorAddDTO;
import com.qlslylq.ad.sdk.model.rsp.AdPlaceWebVO;
import com.qlslylq.ad.sdk.model.rsp.BooleanVO;
import com.qlslylq.ad.sdk.model.rsp.MediaWebVO;
import com.qlslylq.ad.sdk.model.rsp.TimeRecordVO;
import com.qlslylq.ad.sdk.net.Api;
import com.qlslylq.ad.sdk.net.ResponseObject;
import com.qlslylq.ad.sdk.rx.RxUtils;
import com.qlslylq.ad.sdk.rx.subscribers.OnNextOnError;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad.sdk.util.Log;
import com.qlslylq.ad.sdk.util.SPUtils;

/**
 * 仅限SDK内部调用(待限制app端访问)<br/>
 */
public class ApiServiceImpl {

    /**
     * 获取媒体配置信息<br/>
     */
    public static void getMediaConfigList(long appId, OnNextOnError<ResponseObject<MediaWebVO>> listener) {
        Log.dp(String.format("正在获取媒体配置信息：%s", appId));
        RxUtils.rx(Api.getService().getMediaConfigList(appId), new OnNextOnError<ResponseObject<MediaWebVO>>() {
            public void onNext(ResponseObject<MediaWebVO> rsp) {
                listener.onNext(rsp);
            }

            public void onError(ResponseObject rsp) {
                listener.onError(rsp);
            }
        });
    }

    /**
     * 获取广告位的轮换策略<br/>
     */
    public static void getPlaceAdList(long adPlaceId, OnNextOnError<ResponseObject<AdPlaceWebVO>> listener) {
        Log.dp(String.format("正在请求更新广告位策略：%s", adPlaceId));
        RxUtils.rx(Api.getService().getPlaceAdList(adPlaceId), new OnNextOnError<ResponseObject<AdPlaceWebVO>>() {
            public void onNext(ResponseObject<AdPlaceWebVO> rsp) {
                listener.onNext(rsp);
            }

            public void onError(ResponseObject rsp) {
                listener.onError(rsp);
            }
        });
    }

    /**
     * 添加埋点数据<br/>
     */
    public static void addTrack(UserBehaviorAddDTO addDTO) {
        if (AppConst.openSecretBranchLog) {
            Log.dp(String.format("正在上传埋点数据：%s", GsonUtils.gsonString(addDTO)));
        }
        if (addDTO == null || addDTO.getAdSourceId() <= 0) {
            return;
        }
        RxUtils.rx(Api.getService().addTrack(addDTO), new OnNextOnError<ResponseObject<BooleanVO>>() {
            public void onNext(ResponseObject<BooleanVO> rsp) {
            }

            public void onError(ResponseObject rsp) {
            }
        });
    }

    /**
     * 添加埋点数据（系统）<br/>
     */
    public static void addTrack(AppBehaviorAddDTO addDTO) {
        try {
            String json = (String) SPUtils.get(AdMergeSDKApp.getInstance(), addDTO.getBehaviorSign(), null);
            if (!TextUtils.isEmpty(json)) {
                TimeRecordVO item = GsonUtils.getGson().fromJson(json, TimeRecordVO.class);
                if (item != null) {
                    if (BaseMethod.getCurrentDay().equals(item.getTime())) {
                        return;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (AppConst.openSecretBranchLog) {
            Log.dp(String.format("正在上传埋点数据：%s", GsonUtils.gsonString(addDTO)));
        }
        RxUtils.rx(Api.getService().addTrackForApp(addDTO), new OnNextOnError<ResponseObject<BooleanVO>>() {
            public void onNext(ResponseObject<BooleanVO> rsp) {
                TimeRecordVO item = new TimeRecordVO(BaseMethod.getCurrentDay());
                SPUtils.put(AdMergeSDKApp.getInstance(), addDTO.getBehaviorSign(), GsonUtils.gsonString(item));
            }

            public void onError(ResponseObject rsp) {
            }
        });
    }

}